using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MvcSort.Repost;

namespace MvcSort.Models
{
    public class EFrepost<T> : IRinterface<T> where T : BaseEntity
    {   
        private readonly DemoSort _db;
        private readonly DbSet<T> _table;
        public IQueryable<T> Table
        {
            get{
                return _table.AsQueryable<T>();
            }
        }
        public EFrepost()
        {
            _db=new DemoSort();
            _table=_db.Set<T>();
        }

        public void Delete(int id)
        {
            var entity = _table.FirstOrDefault(x=>x.Id==id);
            entity.ISDeleted=true;
            entity.ISActived=false;
            entity.UpdateTime=DateTime.Now;
            _table.Update(entity);
            _db.SaveChanges();
        }

        public void Insert(T entity)
        {
            entity.ISActived=true;
            entity.ISDeleted=false;
            entity.CreateTime=DateTime.Now;
            entity.UpdateTime=DateTime.Now;
            _table.Add(entity);
            _db.SaveChanges();
        }

        public void Update(T entity)
        {
            entity.CreateTime=DateTime.Now;
            _table.Update(entity);
            _db.SaveChanges();
        }
    }
}